/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Distribution.

    Maxsi Distribution is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Distribution is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Distribution. If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiDistributionContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiDistributionContributors.h. For more information please see
	MaxsiDistributionContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiDistribution.dll
	Distributing since 2008

	Data.cpp
	Shared data.

****************************************************************************/

#include "MaxsiEngine.h"
#include "MaxsiDistributionLink.h"
#include "Thread.h"

// INSTALLER DATA
MDLINK	char*	ProductName		=	0;
//MDLINK	char*	ProductVersion	=	0;
MDLINK	List	DownloadName	=	0;
//MDLINK	List	DownloadVersion	=	0;
MDLINK	char*	MasterServer	=	0;
MDLINK	char*	InstallPath		=	0;
MDLINK	char*	SourceModPath	=	0;
MDLINK	char*	SteamUserName	=	0;
MDLINK	char*	Password		=	0;
MDLINK	char*	Developer		=	0;
MDLINK	char*	MapPackFor		=	0;
MDLINK	char*	GameFolder		=	0;
MDLINK	char*	GameFolderReq	=	0;		// If this folder isn't present in the user's folders, don't default to that user.
MDLINK	char*	OnCompleted		=	0;		// If this is set, setup will close at completation and execute this file
MDLINK	char*	Welcome			=	"You are about to install a product using the Maxsi Distribution Technology!";
MDLINK	char*	MDLicense		=	".mis script did not contain a MIS_SetMaxsiDistributionLicense command";
MDLINK	char*	ProductLicense	=	".mis script did not contain a MIS_SetProductLicense command";
MDLINK	char*	ContentArchive	=	0;
MDLINK	char*	ContentType		=	0;
MDLINK	List	MDMirrors		=	0;
MDLINK	char*	MDRoot			=	0;
MDLINK	char*	MDSeekPath		=	0;
MDLINK	char*	MDDistribution	=	0;
MDLINK	char*	MDBinFolder		=	0;
MDLINK	short	MaxsiTorrentPort=	31337;	// TODO: Make this magic constant be defined by the user.
MDLINK	DWORD	ProductPID		=	0;
MDLINK	bool	IsIngame		=	false;
MDLINK	bool	Supported		=	false;	// Whether this product is supported by the Master Content Server.

MDLINK	bool	IsInstall		=	true;	// Is the content being installed for the first time
MDLINK	bool	IsUpdate		=	false;	// Is the content being updated, not installed for the first time
MDLINK	bool	IngameStats		=	true;
MDLINK	bool	bLicense		=	true;
MDLINK	bool	MapPack			=	false;

MISFUNCH(MIS_ProductName);
MISFUNCH(MIS_ProductVersion);
MISFUNCH(MIS_Download);
MISFUNCH(MIS_Username);
MISFUNCH(MIS_InstallPath);
MISFUNCH(MIS_Password);
MISFUNCH(MIS_UpdateProduct);
MISFUNCH(MIS_OnCompleted);
MISFUNCH(MIS_Developer);
MISFUNCH(MIS_NoIngameStatistics);
MISFUNCH(MIS_NoLicense);
MISFUNCH(MIS_MapPack);
MISFUNCH(MIS_MapPackFor);
MISFUNCH(MIS_GameFolder);
MISFUNCH(MIS_RequiresGameFolder);
MISFUNCH(MIS_SetMaxsiDistributionLicense);
MISFUNCH(MIS_SetProductLicense);
MISFUNCH(MIS_SetMaxsiDistributionWelcome);
MISFUNCH(MIS_AddMDMirror);


MISFUNC(MIS_ProductName,MaxsiInstaller){		ProductName		=		BuildString(1,In[0]);	return	0;}
//MISFUNC(MIS_ProductVersion,MaxsiInstaller){		ProductVersion	=		BuildString(1,In[0]);	return	0;}
MISFUNC(MIS_Username,MaxsiInstaller){			SteamUserName	=		BuildString(1,In[0]);	return	0;}
MISFUNC(MIS_InstallPath,MaxsiInstaller){		InstallPath		=		BuildString(1,In[0]);	return	0;}
MISFUNC(MIS_Password,MaxsiInstaller){			Password		=		BuildString(1,In[0]);	return	0;}
MISFUNC(MIS_OnCompleted,MaxsiInstaller){		OnCompleted		=		BuildString(1,In[0]);	return	0;}
MISFUNC(MIS_Developer,MaxsiInstaller){			Developer		=		BuildString(1,In[0]);	return	0;}
MISFUNC(MIS_MapPackFor,MaxsiInstaller){			MapPackFor		=		BuildString(1,In[0]);	return	0;}
MISFUNC(MIS_GameFolder,MaxsiInstaller){			GameFolder		=		BuildString(1,In[0]);	return	0;}
MISFUNC(MIS_RequiresGameFolder,MaxsiInstaller){	GameFolderReq	=		BuildString(1,In[0]);	return	0;}
MISFUNC(MIS_UpdateProduct,MaxsiInstaller){		IsInstall		=		true;
												IsUpdate		=		false;					return	0;}
MISFUNC(MIS_NoIngameStatistics,MaxsiInstaller){	IngameStats		=		false;					return	0;}
MISFUNC(MIS_NoLicense,MaxsiInstaller){			bLicense		=		false;					return	0;}
MISFUNC(MIS_MapPack,MaxsiInstaller){			MapPack			=		true;					return	0;}
MISFUNC(MIS_SetMaxsiDistributionLicense,MaxsiInstaller)
										{		MDLicense		=		BuildString(1,In[0]);	
												str_replace(MDLicense,"\\n","\r\n"); return	0;		}
MISFUNC(MIS_SetProductLicense,MaxsiInstaller)
										{		ProductLicense		=		BuildString(1,In[0]);	
												str_replace(ProductLicense,"\\n","\r\n"); return	0;		}
MISFUNC(MIS_SetMaxsiDistributionWelcome,MaxsiInstaller)
										{		Welcome			=		BuildString(1,In[0]);	
												str_replace(Welcome,"\\n","\r\n"); return	0;		}
MISFUNC(MIS_AddMDMirror,MaxsiInstaller)	{		MDMirrors		=	AddListEntry(MDMirrors,In[0],strlen(In[0])+1);	
												MDMirrors		=	AddListEntry(MDMirrors,In[1],strlen(In[1])+1);	return 0; }
MISFUNC(MIS_Download,MaxsiInstaller)	{		DownloadName	=	AddListEntry(DownloadName,In[0],strlen(In[0])+1);	
												/*DownloadVersion	=	AddListEntry(DownloadVersion,In[1],strlen(In[1])+1);*/	return 0; }

MDLINK	bool	InputApplicationSlashxDashMaxsiDataContainer(char* Input, size_t Size)
{
	size_t	Read	=	0;
	bool	Result	=	0;

	MaxsiDataContainer	MDC;

	while ( Read < Size )
	{
		Result	=	MDC.GetNextVariable(Input,Size-Read,Read);

		if (!Result) { ME_UNDEFINED_BEHAVIOR("InputApplicationSlashxDashMaxsiDataContainer() got an incomplete Application/x-MaxsiDataContainer data stream!"); return false; }

		char*	Variable	=	MDC.Variable();

		if ( _stricmp(Variable,"Product") == 0 )
		{
			// We don't care about this one. We already know this.
		}
		else if ( _stricmp(Variable,"Supported") == 0 )
		{
			if ( _stricmp(MDC.VariableTemp(),"No") == 0 )
			{
				Supported	=	false;
			}
			else if ( _stricmp(MDC.VariableTemp(),"Yes") == 0 )
			{
				Supported	=	true;
			}
		}
		else if ( _stricmp(Variable,"Requires Game Folder") == 0 )
		{
			GameFolderReq	=	MDC.VariableKeep();			
		}
		else if ( _stricmp(Variable,"Map Pack For") == 0 )
		{
			MapPackFor		=	MDC.VariableKeep();			
		}
		else if ( _stricmp(Variable,"Download") == 0 )
		{
			DownloadName	=	AddListEntry(DownloadName,MDC.VariableTemp(),strlen(MDC.VariableTemp())+1);		
		}
		else if ( _stricmp(Variable,"Product-Welcome") == 0 )
		{
			Welcome			=	MDC.VariableKeep();				
		}
		else if ( _stricmp(Variable,"Product-License") == 0 )
		{
			ProductLicense	=	MDC.VariableKeep();				
		}
		else if ( _stricmp(Variable,"MD-License") == 0)
		{
			MDLicense		=	MDC.VariableKeep();	
		}
		else if ( _stricmp(Variable,"ContentType") == 0)
		{
			ContentType		=	MDC.VariableKeep();	
		}
		else
		{
			__debugbreak();
			// Unknown!
		}
	}

	return true;
}